<?php
namespace app\setting\controller;
use think\Request;
use app\common\controller\AdminBase;

class ApiAdminSetting extends AdminBase
{
    public function settingSelect()
    {
        $settings = db('BaseSettings')->select();
        foreach ($settings as $k => $v) {
            if ($v['key'] == 'data') {
                $settingInfo[$v['key']] = $v['val'] ? json_decode($v['val'],true) : [];
            } else {
                $settingInfo[$v['key']] = $v['val'];
            }
        }
        return jsonSuccess('',$settingInfo);
    }
    
    public function debug()
    {
        $debug = input('post.debug');
        if ($debug) {
            file_put_contents(ROOT_PATH . 'runtime' . DS . 'debug.txt', 1);
        } else {
            file_put_contents(ROOT_PATH . 'runtime' . DS . 'debug.txt', 0);
        }
        return jsonSuccess('保存成功');
    }
    
    public function getDebug()
    {
        if (is_file(ROOT_PATH . 'runtime' . DS . 'debug.txt')) {
            $debugStatus = file_get_contents(ROOT_PATH . 'runtime' . DS . 'debug.txt');
        } else {
            $debugStatus = 0;
        }
        return jsonSuccess('保存成功',intval($debugStatus));
    }
    
    public function settingEdit()
    {
        $settingInfo = json_decode(input('post.setting'),true);
        if ($settingInfo) {
            $settingInfo['baiduMipApi'] = trim($settingInfo['baiduMipApi']);
            $settingInfo['baiduMipApiStatus'] = trim($settingInfo['baiduMipApiStatus']);
            $settingInfo['baiduTimeApi'] = trim($settingInfo['baiduTimeApi']);
            $settingInfo['baiduTimeApiStatus'] = trim($settingInfo['baiduTimeApiStatus']);
            $settingInfo['smApi'] = trim($settingInfo['smApi']);
            $settingInfo['smApiStatus'] = trim($settingInfo['smApiStatus']);
            $settingInfo['data'] = json_encode($settingInfo['data']);
        }
        foreach ($settingInfo as $key => $val) {
        	if (db('BaseSettings')->where('key',$key)->find()) {
            	db('BaseSettings')->where( "`key` = '" . $key . "'")->update(array('val' => $val));
        	} else {
           		db('BaseSettings')->insert(array('key' => $key, 'val' => $val));
        	}
            
        }
        return jsonSuccess('保存成功');
    }

    public function settingSave()
    {
        $domain = input('post.domain');
        $siteName = input('post.siteName');
        $httpType = input('post.httpType');
        db('BaseSettings')->where('key','domain')->update(array('val' => $domain));
        db('BaseSettings')->where('key','siteName')->update(array('val' => $siteName));
        db('BaseSettings')->where('key','httpType')->update(array('val' => $httpType));
        if (input('post.setting')) {
            $settingInfo = json_decode(input('post.setting'));
            foreach ($settingInfo as $key => $val) {
                db('BaseSettings')->where( "`key` = '" . $key . "'")->update(array('val' => $val));
            }
        }
        return jsonSuccess('保存成功');
    }

}